package easy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution_937 {

    public String[] reorderLogFiles(String[] logs) {
        String[] array = new String[logs.length];
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        for (int i = 0; i < logs.length; i++) {
            boolean digit = true;
            String[] split = logs[i].split(" ");
            for (int j = 1; j < split.length; j++) {
                String s = split[j];
                char[] chars = s.toCharArray();
                for (char c : chars) {
                    if (c < 48 || c > 57) {
                        digit = false;
                        break;
                    }
                }
                if (!digit) {
                    break;
                }
            }
            if (!digit) {
                list.add(logs[i]);
            } else {
                list1.add(logs[i]);
            }
        }
        list.sort((a, b) -> {
            int index_a = a.indexOf(" ");
            String end_a = a.substring(index_a + 1);

            int index_b = b.indexOf(" ");
            String end_b = b.substring(index_b + 1);
            if (!end_a.equals(end_b)) {
                return end_a.compareTo(end_b);
            } else {
                String start_a = a.substring(0, index_a);
                String start_b = b.substring(0, index_b);
                return start_a.compareTo(start_b);
            }
        });
        int size = list.size();
        for (int i = 0; i < size; i++) {
            array[i] = list.get(i);
        }
        for (int i = 0; i < list1.size(); i++) {
            array[i + size] = list1.get(i);
        }
        return array;
    }

    /*public String[] reorderLogFiles(String[] logs) {
        String[] array = new String[logs.length];
        List<String> list = new ArrayList<>();
        int digitIndex = logs.length - 1;
        for (int i = logs.length -1; i >=0; i--) {
            boolean flag = true, digit = true;
            //            String[] split = logs[i].split(" ");
            char[] chars = logs[i].toCharArray();
            for (char c : chars) {
                if (flag) {
                    if (c == ' ') {
                        flag = false;
                    }
                } else {
                    if (c == ' ') {
                        continue;
                    } else if (c < 48 || c > 57) {
                        digit = false;
                        break;
                    }
                }
            }
            if (digit) {
                array[digitIndex--] = logs[i];
            } else {
                list.add(logs[i]);
            }
        }
        list.sort((a, b) -> {
            int index_a = a.indexOf(" ");
            String end_a = a.substring(index_a + 1);

            int index_b = b.indexOf(" ");
            String end_b = b.substring(index_b + 1);
            if (!end_a.equals(end_b)) {
                return end_a.compareTo(end_b);
            } else {
                String start_a = a.substring(0, index_a);
                String start_b = b.substring(0, index_b);
                return start_a.compareTo(start_b);
            }
        });
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i);
        }
        return array;
    }*/

    public static void main(String[] args) {
        System.out.println('0' - 0);
        System.out.println('9' - 0);
        List<String> list = new ArrayList<>();
        list.add("ab");
        list.add("aa");
        list.sort((a, b) -> {
            return a.compareTo(b);
        });
        String s = "asb fc fd";
        System.out.println(s.indexOf(" "));
        System.out.println(s.substring(0, 3));
        System.out.println(s.substring(4));
        System.out.println(list);

        Solution_937 model = new Solution_937();
        System.out.println(Arrays.toString(model.reorderLogFiles(
            new String[]{"dig1 8 1 5 1", "let1 art can", "dig2 3 6", "let2 own kit dig", "let3 art zero"})));
    }
}
